package com.mm.controller.web.book;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mm.domain.book.DBookSource;
import com.mm.domain.common.DCommonCode;
import com.mm.serivce.book.BookSourceService;
import com.mm.serivce.common.IDCommonCodeService;
import com.mm.util.result.DPage;
import com.mm.util.result.DResult;
import com.mm.vo.web.user.DownTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Description 小说网站
 * @Date 2023/10/11 下午1:27
 * @Author yanglin
 **/
@Api(value = "/book/source/v1/", tags = {"小说网站"})
@Slf4j
@RestController
@RequestMapping("/book/source/v1/")
public class BookSourceController {

    private final BookSourceService bookSourceService;

    public BookSourceController(BookSourceService bookSourceService) {
        this.bookSourceService = bookSourceService;
    }

    /**
     * 小说来源分页
     *
     * @param page
     * @param limit
     * @param sourceTxt
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "page", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "limit", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", dataTypeClass = String.class, name = "sourceTxt", value = "")
    })
    @ApiOperation(value = "小说来源分页", notes = "小说来源分页", httpMethod = "POST")
    @PostMapping("/queryBookSourcePage")
    public DResult queryBookSourcePage(@RequestParam int page, @RequestParam int limit, @RequestParam(required = false) String sourceTxt) {
        Page<DBookSource> bookSourcePage = bookSourceService.queryBookSourcePage(page, limit, sourceTxt);
        DPage<DBookSource> bookSourceDPage = DPage.to(bookSourcePage);
        return DResult.of(bookSourceDPage);
    }

    /**
     * 小说来源明细
     *
     * @param sourceId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", dataTypeClass = Long.class, name = "sourceId", value = "", required = true)
    })
    @ApiOperation(value = "小说来源明细", notes = "小说来源明细", httpMethod = "POST")
    @PostMapping("/queryBookSourceOne")
    public DResult<DBookSource> queryBookSourceOne(@RequestParam Long sourceId) {
        return DResult.of(bookSourceService.queryBookSourceOne(sourceId));
    }

    /**
     * 小说来源编辑
     *
     * @param bookSource
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DBookSource", dataTypeClass = DBookSource.class, name = "bookSource", value = "", required = true)
    })
    @ApiOperation(value = "小说来源编辑", notes = "小说来源编辑", httpMethod = "POST")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/saveOrUpdate")
    public DResult saveOrUpdate(@RequestBody DBookSource bookSource) {
        boolean flag = false;
        if (bookSource.getSourceId() < 0) {
            // 新增
            flag = bookSourceService.save(bookSource);
        } else if (bookSource.getSourceId() > 0) {
            // 更新
            flag = bookSourceService.updateById(bookSource);
        }
        return DResult.of(flag);
    }

    /**
     * 小说来源删除
     *
     * @param sourceId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", dataTypeClass = Long.class, name = "sourceId", value = "", required = true)
    })
    @ApiOperation(value = "小说来源删除", notes = "小说来源删除", httpMethod = "DELETE")
    @DeleteMapping("/delete")
    public DResult delete(@RequestParam Long sourceId) {
        boolean flag = false;
        if (sourceId > 0) {
            flag = bookSourceService.removeBookSource(sourceId);
        }
        return DResult.of(flag);
    }

    /**
     * 爬取小说列表
     *
     * @param sourceId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", dataTypeClass = Long.class, name = "sourceId", value = "", required = true)
    })
    @ApiOperation(value = "爬取小说列表", notes = "爬取小说列表", httpMethod = "GET")
    @GetMapping("/crawlingCategoryBook")
    @Async("asyncPoolTaskExecutor")
    public void crawlingCategoryBook(@RequestParam Long sourceId) {
        bookSourceService.crawlingCategoryBook(sourceId, 10);
    }

    /**
     * 异步爬取小说列表
     */
    @ApiOperation(value = "异步爬取小说列表", notes = "异步爬取小说列表", httpMethod = "GET")
    @GetMapping("/crawlingAllBook")
    @Async("asyncPoolTaskExecutor")
    public void crawlingAllBook() {
        bookSourceService.crawlingAllBook(10);
    }

}
